package com.powergisol.gis.service.impl;

import com.powergisol.gis.dao.CoordinateSystemMapper;
import com.powergisol.gis.entity.CoordinateSystem;
import com.powergisol.gis.exception.GlobalExceptionHandler;
import com.powergisol.gis.service.CoordinateSystemService;
import com.powergisol.gis.vo.CoordinateSystemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CoordinateSystemServiceImpl implements CoordinateSystemService {

    @Autowired
    private CoordinateSystemMapper systemMapper;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    @Override
    public boolean deleteByPrimaryKey(List<Integer> ids) {
        return this.systemMapper.deleteByPrimaryKey(ids) >= ids.size();
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    @Override
    public boolean insert(CoordinateSystem record) {
        //校验数据
        checkData(record);
        return this.systemMapper.insert(record) > 0;
    }

    @Override
    public CoordinateSystem selectByPrimaryKey(Integer id) {
        return this.systemMapper.selectByPrimaryKey(id);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    @Override
    public boolean updateByPrimaryKey(CoordinateSystem record) {
        //校验数据
        checkData(record);
        return this.systemMapper.updateByPrimaryKey(record) > 0;
    }

    @Override
    public List<CoordinateSystem> selectAll(CoordinateSystemVO systemVO) {
        return this.systemMapper.selectAll(systemVO);
    }

    private void checkData(CoordinateSystem record) {
        if (record.getCoorName() == null || record.getCoorName().trim().equals("")) {
            throw new GlobalExceptionHandler(400, "公式名称不能为空");
        } else if (record.getCoorChangeLon() == null || record.getCoorChangeLon().trim().equals("")) {
            throw new GlobalExceptionHandler(400, "经度公式不能为空");
        } else if (record.getCoorChangeLat() == null || record.getCoorChangeLat().trim().equals("")) {
            throw new GlobalExceptionHandler(400, "纬度公式不能为空");
        }
    }

}
